package com.bearingnavi.android.dialogs;

import java.util.Iterator;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.bearingnavi.android.R;
import com.bearingnavi.android.overlays.ArrowOverlay;
import com.bearingnavi.android.overlays.CurrentPositionPointOverlay;
import com.bearingnavi.android.overlays.WalkingPathOverlay;
import com.bearingnavi.android.util.OverlayUtil;
import com.bearingnavi.android.util.StateSaver;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class CleanDialog extends Dialog{

	private Context context;
	private MapView mapView;
	
	public CleanDialog(Context context, MapView mapView) {
		super(context);
		this.context = context;
		this.mapView = mapView;
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		
		this.setContentView(R.layout.clean_dialog);
		Button cleanPath = (Button) findViewById(R.id.cleanPath);
		Button cleanAll = (Button) findViewById(R.id.cleanAll);	
		
		
		cleanPath.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View view) {
				
				com.bearingnavi.android.dialogs.Dialog.buildConfirmUndoDialog(context, new com.bearingnavi.android.dialogs.Dialog.ConfirmHandler() {
					
					public void onConfirm() {
						
						OverlayUtil.cleanWalkingPath(mapView);
						CleanDialog.this.dismiss();
					}
				});
			}
		});
		
		
		cleanAll.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View view) {
				
				com.bearingnavi.android.dialogs.Dialog.buildConfirmUndoDialog(context, new com.bearingnavi.android.dialogs.Dialog.ConfirmHandler() {
					
					public void onConfirm() {
						
						Iterator<Overlay> iterator = mapView.getOverlays().iterator();
						while (iterator.hasNext()) {
							Overlay overlay = (Overlay) iterator.next();
							
							if (overlay instanceof ArrowOverlay ||
									overlay instanceof CurrentPositionPointOverlay) {
								continue;
							}
							if (overlay instanceof WalkingPathOverlay) {
								((WalkingPathOverlay) overlay).clean();
								continue;
							}
							
							iterator.remove();
						}
						
						
						mapView.invalidate();
						StateSaver.getInstance().marshalOverlays(mapView.getOverlays());
						CleanDialog.this.dismiss();
					}
				});
			}
		});
				
	}

}
